package com.carkeeper.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.BillDTO;
import com.carkeeper.entity.BillPO;
import com.carkeeper.entity.OrderPO;
import com.carkeeper.service.BillService;
import com.carkeeper.service.OrderService;
import com.carkeeper.vo.BillVO;
import com.carkeeper.vo.Pager;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/bill")
public class BillController {
    @Autowired
    private BillService billService;
    @Autowired
    private OrderService orderService;

    /*
     * 新增账单
     * */
    @PostMapping()
    public String addBill(@RequestBody BillPO billPOByParam) {
        BillPO billPO = new BillPO();
        if (billPOByParam.getOrderId() != null) {
            OrderPO order = orderService.getOrder(billPOByParam.getOrderId());
            if (order != null) {
                billPO.setOrderId(order.getId());
                billPO.setStoreId(order.getStoreId());
                billPO.setStoreName(order.getStoreName());
                billPO.setPrice(order.getMoney());
                billPO.setSummary(billPOByParam.getSummary());
                billPO.setSubtypeName(billPOByParam.getSubtypeName());
                billPO.setPayState(1);
            }
        }
        if (billPO.getOrderId() != null) {
            try {
                billService.addBill(billPO);
                return "新增成功";
            } catch (Exception e) {
                e.printStackTrace();
                return "新增失败";
            }
        } else {
            return "订单不存在";
        }
    }

    /*
     * 获取账单详情
     * */
    @GetMapping("/{id}")
    public BillPO getBill(@PathVariable("id") Long id) {
        BillPO bill = billService.getBill(id);
        return bill;
    }

    /*
     * 删除账单
     * */
    @DeleteMapping("/{id}")
    public String delById(@PathVariable("id") Long id) {
        try {
            billService.delById(id);
            return "删除成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
    }

    /*
     * 获取账单分页数据
     * */
    @GetMapping
    public Pager<BillPO> getBillPage(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(name = "type") Integer type,
                                     @RequestParam(name = "code", required = false) String code,
                                     @RequestParam(name = "storeName", required = false) String storeName,
                                     @RequestParam(name = "payState", required = false) Integer payState,
                                     @RequestParam(name = "createTimeStart", required = false) String createTimeStart,
                                     @RequestParam(name = "createTimeEnd", required = false) String createTimeEnd,
                                     @RequestParam(name = "storeId", required = false) Long storeId) {
        Page<BillPO> billPage = billService.getBillPage(page, pageSize, type, code, storeName, payState, createTimeStart, createTimeEnd, storeId);
        Pager<BillPO> pager = new Pager<>(billPage);
        return pager;
    }

    @GetMapping("/list")
    public List<BillDTO> findAll(@RequestParam(name = "storeId", required = false) Long storeId,
                                 @RequestParam(name = "payState", required = false) Integer payState) {
        return billService.findAll(storeId, payState).stream().map(po -> {
            BillDTO dto = new BillDTO();
            BeanUtils.copyProperties(po, dto);
            return dto;
        }).collect(Collectors.toList());
    }
}
